library(estimatr) 
library(ggplot2)
library(dplyr)
library(dotwhisker)
library(gridExtra)
library(coefplot)
library(tidyverse)


# Figure 8: Minimum Likelihood of Success, by Nuclear Theory

setwd("/Users/dmin/Dropbox/Replication_Data/Paper_2/")

Dat <- read.csv("Paper_2_Data_Clean.csv", stringsAsFactors = FALSE)

gg_df <-
  Dat %>%
  group_by(NuclearTheory) %>%
  summarize(tidy(
    lm_robust(
      outcome_custom ~ 1,
      data = cur_data()
    )
  ))

theorylabels<-c("Never-Ever","Existential\nDeterrence","Offense/\nDefense","Utilitarian" )

p<- ggplot(data=Dat, aes(x=as.factor(NuclearTheory), y=outcome_custom, fill = as.factor(NuclearTheory)), inherit_aes=FALSE) + 
  geom_jitter(width=.3, height=3, alpha=0.2)+
  geom_point(data=gg_df, aes(x=NuclearTheory, y=estimate, size=5, color=as.factor(NuclearTheory), stroke=0), inherit.aes = FALSE)+
  geom_errorbar(data=gg_df, aes(x=NuclearTheory, ymin=conf.low, ymax=conf.high, width = .5, color=as.factor(NuclearTheory), size=2), inherit.aes = FALSE) +
  scale_x_discrete(labels=theorylabels) + 
  scale_color_manual(values=c("1"="green3","2"="yellow3","3"="orange3", "4"="red3")) + 
  theme_bw() + xlab("Nuclear Theory") + 
  ylab("Minimum Likelihood of Success to Approve Nuclear Use") + 
  theme(legend.position="none")

g <- p + annotate("rect", xmin=-.2, xmax = 5, ymin=105, ymax = 114, alpha=.2, fill="blue" ) + annotate("text", x=0.2, y=110, label="Never") +
  coord_cartesian(ylim=c(-10,110))

g

ggsave("Image_8.png", plot=g, width=2500, height=2500, units=c("px"))


       
       